/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.text.DecimalFormat;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import com.toedter.calendar.*;
import java.util.Date;
import com.beans.Entrees;
import com.beans.Projets;
import com.beans.Sorties;
import com.beans.Users;

import com.metier.EntreesMetier;
import com.metier.ProjetMetier;
import com.metier.SortiesMetier;

/**
 * 
 * @author Mamadou
 */

/**
 * 
 * @author Mamadou
 */
public class PanEntres extends javax.swing.JPanel implements MouseListener {

	private javax.swing.JButton boutonAjouter;
	private javax.swing.JButton boutonImprimer;
	private javax.swing.JButton boutonModifier;
	private javax.swing.JButton boutonRechercher;
	private javax.swing.JButton boutonSupprimer, boutonDetails;
	private javax.swing.JLabel jLabel1, textLabel;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JPanel panelBoutons, panelBouton1;
	private javax.swing.JPanel panelbas;
	private javax.swing.JPanel panelhaut;
	private JTextField textRechercher;
	private JLabel labelDateDebut, labelDateFin;
	private com.toedter.calendar.JDateChooser dateDebutProjet;
	private com.toedter.calendar.JDateChooser dateFinProjet;
	DefaultTableModel dtm;
	EntreesMetier entreeMetier;
	JTable tableListeEntrees;
	private JButton boutonAnnulerAjoutEntree, boutonAnnulerModif;

	// End of variables declaration//GEN-END:variables

	public PanEntres() {

		entreeMetier = new EntreesMetier();
		initComponents();
		getTableListeEntrees().addMouseListener(this);
		boutonSupprimer.setEnabled(false);
		boutonModifier.setEnabled(false);

	}

	private void initComponents() {

		panelhaut = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		panelbas = new javax.swing.JPanel();
		panelbas.setLayout(new GridLayout(1, 1));
		panelBoutons = new javax.swing.JPanel();
		panelBouton1 = new JPanel();
		labelDateDebut = new JLabel("DATE DEBUT");
		labelDateFin = new JLabel("DATE FIN");

		boutonAjouter = new javax.swing.JButton();
		boutonSupprimer = new javax.swing.JButton();
		boutonModifier = new javax.swing.JButton();
		boutonImprimer = new javax.swing.JButton();
		boutonImprimer.setVisible(false);
		boutonRechercher = new javax.swing.JButton();
		boutonDetails = new JButton();
		boutonAnnulerAjoutEntree = new JButton("Annuler");
		boutonAnnulerModif = new JButton("Annuler");
		boutonAnnulerModif.setFont(new java.awt.Font("Verdana", 3, 14));

		jLabel2 = new javax.swing.JLabel();
		textLabel = new JLabel("");
		textRechercher = new JTextField();
		dateDebutProjet = new com.toedter.calendar.JDateChooser();
		dateFinProjet = new com.toedter.calendar.JDateChooser();
		labelDateDebut.setFont(new java.awt.Font("Verdana", 3, 12));
		labelDateFin.setFont(new java.awt.Font("Verdana", 3, 12));
		labelDateDebut.setForeground(new java.awt.Color(255, 255, 255));
		labelDateFin.setForeground(new java.awt.Color(255, 255, 255));

		setBackground(new java.awt.Color(128, 130, 133));
		setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1,
				new java.awt.Color(102, 153, 255)));
		setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		setMaximumSize(new java.awt.Dimension(1050, 550));
		setMinimumSize(new java.awt.Dimension(1050, 550));
		setLayout(new java.awt.BorderLayout());
		panelBoutons.setLayout(new BorderLayout());
		panelBouton1.setBackground(new java.awt.Color(128, 130, 133));
		panelBoutons.setBackground(new java.awt.Color(128, 130, 133));
		boutonDetails.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		boutonDetails.setText("DETAILS");
		boutonDetails.setVisible(false);
		boutonDetails.setBackground(new java.awt.Color(223, 242, 255));

		panelhaut.setBackground(new java.awt.Color(128, 130, 133));
		panelhaut.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1,
				1, 1, new java.awt.Color(103, 153, 255)));
		panelhaut.setPreferredSize(new java.awt.Dimension(1050, 75));

		jLabel1.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/GESTION DES ENTRÉES.jpg")); // NOI18N

		panelbas.setBackground(new java.awt.Color(223, 242, 255));
		panelbas.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1,
				1, new java.awt.Color(102, 153, 255)));
		panelbas.setMaximumSize(new java.awt.Dimension(1050, 375));
		panelbas.setMinimumSize(new java.awt.Dimension(1050, 375));
		panelbas.setPreferredSize(new java.awt.Dimension(1056, 410));

		panelBoutons.setPreferredSize(new java.awt.Dimension(742, 35));
		panelBoutons.setBorder(javax.swing.BorderFactory.createMatteBorder(1,
				1, 1, 1, new java.awt.Color(103, 153, 255)));
		panelBoutons.setMaximumSize(new java.awt.Dimension(1050, 50));
		panelBoutons.setMinimumSize(new java.awt.Dimension(1050, 50));
		panelBoutons.setPreferredSize(new java.awt.Dimension(1050, 40));

		boutonAjouter.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/add16.png")); // NOI18N
		boutonAjouter.setToolTipText("Ajouter");
		boutonAnnulerAjoutEntree.setFont(new java.awt.Font("Verdana", 3, 14));
		boutonAnnulerAjoutEntree.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				boutonAjouter.setVisible(true);
				boutonSupprimer.setVisible(true);
				boutonImprimer.setVisible(false);
				boutonRechercher.setVisible(true);
				boutonModifier.setVisible(true);
				textRechercher.setVisible(true);
				dateDebutProjet.setVisible(true);
				dateFinProjet.setVisible(true);
				boutonDetails.setVisible(true);
				labelDateDebut.setVisible(true);
				labelDateFin.setVisible(true);
				panelbas.removeAll();
				panelbas.add(new JScrollPane(tableListeEntrees),
						BorderLayout.CENTER);
				validate();
			}
		});
		boutonSupprimer.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/supprimer.gif"));
		boutonSupprimer.setToolTipText("Supprimer");
				boutonModifier.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/modifier.gif"));
		boutonModifier.setToolTipText("Modifier");
		boutonModifier.addActionListener(new ActionListener() {
			
		
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				chargePanEntreeSelection();
			}
		});

		boutonImprimer.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/imprimer.gif"));
		boutonImprimer.setToolTipText("Imprimer");

		boutonRechercher.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/recherchermod.png"));
		boutonRechercher.setToolTipText("Rechercher");

		jLabel2.setFont(new java.awt.Font("Verdana", 3, 14));

		panelhaut.add(jLabel1);

		// jLabel2.setText("LISTE DES PROJETS");
		textRechercher.setPreferredSize(new Dimension(100, 27));
		textRechercher.setText("Référence");

		panelBouton1.add(boutonAjouter);
		panelBouton1.add(boutonSupprimer);
		panelBouton1.add(boutonModifier);

		panelBouton1.add(boutonImprimer);
		panelBouton1.add(boutonRechercher);
		panelBouton1.add(textRechercher);
		panelBouton1.add(labelDateDebut);
		panelBouton1.add(dateDebutProjet);
		panelBouton1.add(labelDateFin);
		panelBouton1.add(dateFinProjet);
		panelBouton1.add(boutonDetails, BorderLayout.EAST);
		// panelBouton1.add(textLabel);
		// panelBouton1.add(jLabel2, BorderLayout.CENTER);
		panelBoutons.add(panelBouton1, BorderLayout.WEST);

		add(panelBoutons, java.awt.BorderLayout.CENTER); // on fait appel à
															// cette méthode
															// pour le
		add(panelhaut, BorderLayout.NORTH);
		tableauListeEntrees();
		panelbas.add(new JScrollPane(tableListeEntrees));
		add(panelbas, BorderLayout.SOUTH);
		boutonSupprimer.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				final int[] selectionSupprimer = getTableListeEntrees()
						.getSelectedRows();

				if (selectionSupprimer.length == 0) {
					JOptionPane.showMessageDialog(null,
							"veuillez selectionner une entrée d'abord!!!!");
				} else {
					if (JOptionPane.showConfirmDialog(getFrame(),
							"Voulez-vous vraiment supprimer ?", "Information",
							JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
						testDeleteEntree();

					}

				}

			}
		});
		boutonAjouter.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				jLabel2.setVisible(false);
				boutonAjouter.setVisible(false);
				boutonSupprimer.setVisible(false);
				boutonImprimer.setVisible(false);
				boutonRechercher.setVisible(false);
				boutonModifier.setVisible(false);
				textRechercher.setVisible(false);
				dateDebutProjet.setVisible(false);
				dateFinProjet.setVisible(false);
				boutonDetails.setVisible(false);
				labelDateDebut.setVisible(false);
				labelDateFin.setVisible(false);
				
				panelbas.removeAll();
				PanAjouterEntrees pan = new PanAjouterEntrees(
						boutonAnnulerAjoutEntree);
				panelbas.add(pan, BorderLayout.SOUTH);
				validate();

			}
		});
	}

	public JTextField getTextRechercher() {
		return textRechercher;
	}

	public void setTextRechercher(JTextField textRechercher) {
		this.textRechercher = textRechercher;
	}

	// methode qui permet de charger la liste des projets dans un JTable

	public void tableauListeEntrees() {

		String[] entetes = { "Réference", "Numero", "Type", "Montant",
				"Date de Création" };
		entreeMetier = new EntreesMetier();
		List<Entrees> listEntrees = entreeMetier.chargerAllEntrees();
		int nombreLigne = listEntrees.size();
		System.out.println("taille de la liste" + listEntrees.size());
		Object data[][] = new Object[nombreLigne][5];

		// On parcoure la liste pour recuperer les projets et remplir le tableau
		// d'objet
		for (int i = 0; i < nombreLigne; i++) {
			Entrees entrees = new Entrees();

			entrees = listEntrees.get(i);

			data[i][0] = entrees.getEntRef();

			data[i][1] = entrees.getEntNum();

			data[i][2] = entrees.getEntType();
			String montant=new DecimalFormat("0").format(entrees.getEntMontant()) ;// renvoie un String
			data[i][3] = montant;

			//data[i][3] = entrees.getEntMontant();

			data[i][4] = entrees.getEntDateCreate();

		}
		dtm = new DefaultTableModel(data, entetes);
		tableListeEntrees = new JTable(dtm);

	}

	public JPanel getFrame() {

		return this;

	}

	// methode pour la suppression d'une sortie

	public void testDeleteEntree() {

		// on recupére le nombre de ligne selectionné pour la suppression

		final int[] selection = getTableListeEntrees().getSelectedRows();
		System.out.println("voici la taille selectionné " + selection.length);
		// test pour voir si la selection est vide
		if (selection.length == 0) {
			JOptionPane.showMessageDialog(null,
					"veuillez selectionner une entrée d'abord!!!!");

		} else {// si la selection n'est pas vide
			int taille = selection.length;

			for (int i = 0; i < taille; i++) {
				int index = selection[i];

				String entreeReference = (String) getTableListeEntrees()
						.getValueAt(index, 0);
				System.out
						.println("voici la reférence de l'entrée à supprimer "
								+ entreeReference);

				Entrees entreeSupprimer = entreeMetier
						.findEntreByReference(entreeReference);
				entreeMetier.testDeleteEntre(entreeSupprimer);

				dtm.removeRow(index);

			}

		}
	}

	public JTable getTableListeEntrees() {
		return tableListeEntrees;
	}

	public void setTableListeSorties(JTable tableListeEntrees) {
		this.tableListeEntrees = tableListeEntrees;
	}

	// méthode pour le chargement des données dans le panel modifier entrée

	public void chargePanEntreeSelection() {

		int ligneSelectionner = getTableListeEntrees().getSelectedRow();
		if (ligneSelectionner == -1) {
			JOptionPane.showMessageDialog(null,
					"veuillez selectionner une entrée d'abord!!!!");

		} else {
			Entrees entrees = new Entrees();
			EntreesMetier entreesMetier = new EntreesMetier();
			String referenceRecuperer = (String) getTableListeEntrees()
					.getValueAt(ligneSelectionner, 0);
			String numeroRecuperer = (String) getTableListeEntrees()
					.getValueAt(ligneSelectionner, 1);
			String typeRecuperer = (String) getTableListeEntrees().getValueAt(
					ligneSelectionner, 2);
		 String montantRecuperer = (String) getTableListeEntrees()
					.getValueAt(ligneSelectionner, 3);
			Date dateCreateRecuperer = (Date) getTableListeEntrees()
					.getValueAt(ligneSelectionner, 4);
			entrees = entreesMetier.findEntreByReference(referenceRecuperer);
			Date entreeDate = entrees.getEntDate();
			Date entreeDateEcheance = entrees.getEntDateEcheance();
			String responsable = entrees.getEntUserCreate();
			boutonAjouter.setVisible(false);
			boutonSupprimer.setVisible(false);
			boutonImprimer.setVisible(false);
			boutonRechercher.setVisible(false);
			boutonModifier.setVisible(false);
			textRechercher.setVisible(false);
			dateDebutProjet.setVisible(false);
			dateFinProjet.setVisible(false);
			boutonDetails.setVisible(false);
			labelDateDebut.setVisible(false);
			labelDateFin.setVisible(false);

			panelbas.removeAll();
			// charement des champs dans le panel Modifier entrée
			PanModifierEntrees panModifEntrees = new PanModifierEntrees(
					boutonAnnulerAjoutEntree);
			panModifEntrees.getjTextFieldNumero().setText(numeroRecuperer);
			panModifEntrees.getjTextFieldReference()
					.setText(referenceRecuperer);
			panModifEntrees.getjTextFieldUserCreator().setText(responsable);
			panModifEntrees.getjTextFieldMontant().setText(
					montantRecuperer);
			panModifEntrees.getDateCreation().setDate(dateCreateRecuperer);
			panModifEntrees.getDateEcheance().setDate(entreeDateEcheance);
			panModifEntrees.getDateExate().setDate(entreeDate);
			panelbas.add(panModifEntrees, BorderLayout.SOUTH);
			validate();

		}

	}

	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
		if(getTableListeEntrees().getRowSelectionAllowed()==true){
			boutonSupprimer.setEnabled(true);
			boutonModifier.setEnabled(true);
		}
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}
